package com.school.hjt.schoolclasscard.fragment
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.LinearLayout
import android.widget.TextView



import com.school.hjt.schoolclasscard.R
import com.school.hjt.schoolclasscard.adapter.MemberFingerDataModel
import com.school.hjt.schoolclasscard.adapter.MemberFingerprintAdapter
import com.school.hjt.schoolclasscard.adapter.RecycleViewDivider
import com.school.hjt.schoolclasscard.db.SchArticle
import com.school.hjt.schoolclasscard.fragment.BaseFragment
import com.school.hjt.schoolclasscard.model.DongFModel
import com.school.hjt.schoolclasscard.ui.DongActivity
import com.school.hjt.schoolclasscard.utlis.Constant
import com.youth.banner.Banner
import com.youth.banner.Transformer
import java.util.*

class Tab1Fragment : BaseFragment(){
    override fun initView(view: View?) {
        rvSeckillFingerprint = view!!.findViewById(R.id.rvSeckillFingerprint)
    }
    var model: DongFModel?=null
    var receiver:FDongReceiver?=null
    val list=ArrayList<SchArticle>()
    val list1=ArrayList<SchArticle>()
    var isNo:Boolean?=false
    var post=-1
    override fun loadData() {
        model= DongFModel()
        receiver=FDongReceiver()
        activity.registerReceiver(receiver,filter())
        val linearLayoutManager = LinearLayoutManager(activity)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        rvSeckillFingerprint!!.layoutManager = linearLayoutManager

        rvSeckillFingerprint!!.addItemDecoration(RecycleViewDivider(
                activity, LinearLayoutManager.VERTICAL, 11, resources.getColor(R.color.abc_background_cache_hint_selector_material_dark)))
        val pool1 = rvSeckillFingerprint!!.recycledViewPool
        pool1.setMaxRecycledViews(0, 0)
        rvSeckillFingerprint!!.recycledViewPool = pool1
//        val list=ArrayList<MemberFingerDataModel>()
        list+=model!!.schArticle
        list1+=model!!.schArticle
        memberFingerprintAdapter = MemberFingerprintAdapter(activity, list)
//        memberFingerprintAdapter!!.setOnItemClickLitener({ _, position ->
//            isNo=true
//            if(post!=-1&&post!=position){
//                list[post].setIsClose(false)
//                memberFingerprintAdapter!!.notifyItemChanged(post)
//            }
//            memberFingerprintAdapter!!.setCurrentPosition(position)
//            list[position].setIsClose(!list[position].isClose)
//            post=position
//            memberFingerprintAdapter!!.notifyItemChanged(position)
//        })
        memberFingerprintAdapter!!.setOnItemClickLitener { view, position ->
            val intent=Intent(activity, DongActivity::class.java)
            intent.putExtra("web",list[position].link)
            intent.putExtra("title",list[position].title)
            activity.startActivity(intent)
        }
        rvSeckillFingerprint!!.adapter = memberFingerprintAdapter
    }

    override fun getLayoutResource(): Int {
        return R.layout.fragment_dongtai
    }
    var memberFingerprintAdapter: MemberFingerprintAdapter? = null
    private var rvSeckillFingerprint: RecyclerView? = null
    /**
     * 广播类
     */
    inner class FDongReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            when (action) {
               Constant.ARTICLR->{
//                   Log.e("ATG","进来动态")
                   list.clear()
                   list+=model!!.schArticle
                   memberFingerprintAdapter!!.notifyDataSetChanged()
               }
                Constant.SCHPOSTION->{
                    val position= intent.getIntExtra(Constant.SCHPOSTION,0)
                    list[position].setIsClose(true)
                    rvSeckillFingerprint!!.smoothScrollToPosition(position)
                    memberFingerprintAdapter!!.notifyItemChanged(position)
                }
            }
        }
    }
    /**
     * @return IntentFilter 广播
     */
    private fun filter(): IntentFilter {
        val filter = IntentFilter()
        filter.addAction(Constant.ARTICLR)
        filter.addAction(Constant.SCHPOSTION)
        return filter
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {

        if (isNo==true){
            Log.e("ATG","走到")
            for (i in 0 until list.size) {
                list[i].setIsClose(false)
            }
        memberFingerprintAdapter!!.notifyDataSetChanged()
        }

        super.setUserVisibleHint(isVisibleToUser)
    }

}